package cn.ac.iie.pre.parser.put;

import cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created by Black-Laptop on 6/11/2017.
 */
public class RocketMQUtils {

    private static final Logger LOG = LoggerFactory.getLogger(RocketMQUtils.class);

    public static Object[] getDataFromJson(JSONObject jsonObject, List<String> columns, List<REFieldType> types) {
        List<Object> data = new ArrayList<>();
        for (int i = 0; i < types.size(); i++) {
            Object data_temp = getSingleDataFromJson(jsonObject, columns.get(i), types.get(i));
            data.add(data_temp);
        }
        data.set(data.size() - 1, System.currentTimeMillis() / 1000);
        return data.toArray();
    }

    private static Object getSingleDataFromJson(JSONObject jsonObject, Object column, REFieldType type) {
        Object data = null;
        // LOG.info("type: " + type.toString());
        try {
            switch (type) {
                case Int: {
                    LOG.debug("value: " + jsonObject.getInt((String) column));
                    data = jsonObject.getInt((String) column);
                    break;
                }
                case Long: {
                    LOG.debug("value: " + jsonObject.getLong((String) column));
                    data = jsonObject.getLong((String) column);
                    break;
                }
                case String: {
                    LOG.debug("value: " + jsonObject.getString((String) column));
                    data = jsonObject.getString((String) column);
                    break;
                }
                case Strings: {
                    LOG.debug("value: " + jsonObject.getJSONArray((String) column));
                    JSONArray jsonArray = jsonObject.getJSONArray((String) column);
                    data = jsonArray.toArray(new String[jsonArray.size()]);
                    break;
                }
                case Struct: {
                    Map<String, Object> mapValue = new HashMap<>();
//                    String columnValue = "";
//                    columnValue = jsonObject.getString((String) column);
//                    JSONObject jsonValue = JSONObject.fromObject(columnValue);
//                    Iterator keyIterator = jsonValue.keys();
//
//                    while (keyIterator.hasNext()) {
//                        String key = (String) keyIterator.next();
//                        Object value = jsonValue.get(key);
//                        mapValue.put(key, value);
//                    }
//                    mapValue = (Map) (Object)jsonObject.getString((String) column);
                    mapValue.put("tct_l1", 1);
                    mapValue.put("tct_l2", 2);
                    mapValue.put("tct_l3", -1);
                    data = mapValue;
                    LOG.debug("value: " + mapValue);
                    break;
                }
                case Structs: {
//                    String columnValue = "";
//                    if (GlobalParas.PARAMTERS_NEED_URL_DECOD) {
//                        columnValue = java.net.URLDecoder.decode(jsonObject.getString((String) column));
//                    } else {
//                        columnValue = jsonObject.getString((String) column);
//                    }
//                    JSONArray jsonValueArray = JSONArray.fromObject(columnValue);
//                    Map[] mapValue = new Map[jsonValueArray.size()];
//                    for (int i = 0; i < jsonValueArray.size(); i++) {
//                        mapValue[i] = new HashMap<>();
//                        JSONObject jsonValue = jsonValueArray.getJSONObject(i);
//                        Iterator keyIterator = jsonValue.keys();
//
//                        while (keyIterator.hasNext()) {
//                            String key = (String) keyIterator.next();
//                            Object value = jsonValue.get(key);
//                            mapValue[i].put(key, value);
//                        };
//                    }
                    Map[] mapValue = new Map[3];
                    mapValue[0] = new HashMap<String, Object>();
                    mapValue[0].put("tct_l1", 1);
                    mapValue[1] = new HashMap<String, Object>();
                    mapValue[1].put("tct_l2", 2);
                    mapValue[2] = new HashMap<String, Object>();
                    mapValue[2].put("tct_l3", -1);
                    data = mapValue;
                    LOG.debug("value: " + Arrays.toString(mapValue));
                    break;
                }
                default: {
                    LOG.error("not find type: " + type);
                    break;
                }
            }
        } catch (Exception e) {
            LOG.error("set value error type: (). ()", type, ExceptionUtils.getFullStackTrace(e));
        }
        return data;
    }

    public static REFieldType GetVarType(Object object) {
        String type = object.getClass().getSimpleName();
        REFieldType fieldType = null;
        switch (type) {
            case "String": {
                fieldType = REFieldType.String;
                break;
            }
            case "Integer": {
                fieldType = REFieldType.Int;
                break;
            }
            case "Long": {
                fieldType = REFieldType.Long;
                break;
            }
            default: {
                LOG.error("type not found: " + type);
            }
        }
        return fieldType;
    }


}
